package com.dmap.admin.controller.backgroundv3;

import com.dmap.admin.constants.VersionConstant;
import com.dmap.admin.controller.backgroundv3.param.AndroidVersionEditParam;
import com.dmap.admin.controller.backgroundv3.param.AndroidVersionParam;
import com.dmap.admin.controller.backgroundv3.param.AndroidVersionPublishParam;
import com.dmap.admin.po.AndroidVersion;
import com.dmap.admin.service.backgroundv3.AndroidVersionService;
import com.dmap.admin.vo.backgroundv3.AndroidVersionInfoVo;
import com.dmap.admin.vo.backgroundv3.AndroidVersionSelectVo;
import com.dmap.base.constants.Constant;
import com.dmap.base.vo.BaseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: zmh
 * @create: 18-8-22
 * @description: 安卓版本controller
 **/
@Api(value = "安卓版本", description = "background/安卓版本")
@RestController
@RequestMapping(value = "/background/dmap-knowledge-server/v1/androidVersion/")
public class AndroidVersionBackController {
    @Resource
    private AndroidVersionService androidVersionService;

    @ResponseBody
    @ApiOperation(value = "新增安卓版本", notes = "新增安卓版本")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public BaseVo insertAndroidVersion(@RequestBody AndroidVersionParam param) {
        BaseVo result = new BaseVo();
        if (StringUtils.isBlank(param.getAppNumber()) || StringUtils.isBlank(param.getDescription()) || StringUtils.isBlank(param.getDescriptionEn()) ||
                StringUtils.isBlank(param.getJsonUrl()) || StringUtils.isBlank(param.getMd5()) || StringUtils.isBlank(param.getMinVersion()) ||
                StringUtils.isBlank(param.getMinVersionCode()) || StringUtils.isBlank(param.getNewVersion()) || StringUtils.isBlank(param.getNewVersionCode()) ||
                param.getStatus() == 0 || StringUtils.isBlank(param.getUrl())) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = androidVersionService.insertAndroidVersion(param);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_INSERT_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_INSERT_ERROR_MSG);
        }
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "修改安卓版本", notes = "修改安卓版本")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseVo updateAndroidVersion(@RequestBody AndroidVersionEditParam param) {
        BaseVo result = new BaseVo();
        if (!StringUtils.isNumeric(param.getAndroidVersionId()) || StringUtils.isBlank(param.getAndroidVersionId()) || StringUtils.isBlank(param.getAppNumber()) || StringUtils.isBlank(param.getDescription()) || StringUtils.isBlank(param.getDescriptionEn()) ||
                StringUtils.isBlank(param.getJsonUrl()) || StringUtils.isBlank(param.getMd5()) || StringUtils.isBlank(param.getMinVersion()) ||
                StringUtils.isBlank(param.getMinVersionCode()) || StringUtils.isBlank(param.getNewVersion()) || StringUtils.isBlank(param.getNewVersionCode()) ||
                param.getStatus() == 0 || StringUtils.isBlank(param.getUrl())) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = androidVersionService.updateAndroidVersion(param);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_UPDATE_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_UPDATE_ERROR_MSG);
        }
        if (success == 2) {
            result.setCode(VersionConstant.RESPONSE_ANNDROID_VERSION_EXIST_ERROR_CODE);
            result.setMsg(VersionConstant.RESPONSE_ANNDROID_VERSION_EXIST_ERROR_MSG);
        }
        return result;
    }


    @ResponseBody
    @ApiOperation(value = "发布安卓版本", notes = "发布安卓版本")
    @RequestMapping(value = "/publish", method = RequestMethod.POST)
    public BaseVo updateAndroidVersionPublish(@RequestBody AndroidVersionPublishParam param) {
        BaseVo result = new BaseVo();
        if (!StringUtils.isNumeric(param.getAndroidVersionId()) || StringUtils.isBlank(param.getAndroidVersionId())
                || !StringUtils.isNumeric(param.getPromulgatorId()) || StringUtils.isBlank(param.getPromulgatorId())
        ) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = androidVersionService.updateAndroidVersionPublish(param);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_UPDATE_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_UPDATE_ERROR_MSG);
        }
        if (success == 2) {
            result.setCode(VersionConstant.RESPONSE_ANNDROID_VERSION_EXIST_ERROR_CODE);
            result.setMsg(VersionConstant.RESPONSE_ANNDROID_VERSION_EXIST_ERROR_MSG);
        }
        return result;
    }

    @Transactional
    @ResponseBody
    @ApiOperation(value = "删除安卓版本", notes = "删除安卓版本")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public BaseVo deleteAndroidVersion(@RequestParam String androidVersionIds) {
        BaseVo result = new BaseVo();
        if (StringUtils.isBlank(androidVersionIds)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = androidVersionService.deleteAndroidVersionList(androidVersionIds);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_DELETE_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_DELETE_ERROR_MSG);

        }
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "查询安卓版本", notes = "查询安卓版本")
    @RequestMapping(value = "/select", method = RequestMethod.GET)
    public BaseVo selectAndroidVersion(@RequestParam(required = false) String companyNumber,
                                       @RequestParam(required = false) String appNumber,
                                       @RequestParam String lang,
                                       @RequestParam(required = false) String columnId,
                                       @RequestParam(required = false) String key,
                                       @RequestParam(required = false, defaultValue = "0") long order,
                                       @RequestParam int pageIndex,
                                       @RequestParam int pageSize) {
        BaseVo result = new BaseVo();
//        if (lang == null || pageIndex == 0 || pageSize == 0) {
//            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
//            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
//            return result;
//        }
//        if (appNumber != null && StringUtils.isBlank(appNumber)) {
//            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
//            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
//            return result;
//        }
//        if (companyNumber != null && StringUtils.isBlank(companyNumber)) {
//            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
//            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
//            return result;
//        }
//        if (columnId != null && !StringUtils.isNumeric(columnId)) {
//            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
//            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
//            return result;
//        }
//        if (StringUtils.isBlank(lang)) {
//            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
//            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
//            return result;
//        }
        List<AndroidVersion> androidVersionList = androidVersionService.selectAndroidVersion();
        List<AndroidVersionSelectVo> androidVersionSelectVoList = new ArrayList<>();
        for(AndroidVersion androidVersion  : androidVersionList){
            AndroidVersionSelectVo androidVersionSelectVo = new AndroidVersionSelectVo(androidVersion);
            androidVersionSelectVoList.add(androidVersionSelectVo);
        }
        result.setData(androidVersionSelectVoList);
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "查看安卓版本信息", notes = "查看安卓版本信息")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public BaseVo getAndroidVersion(@RequestParam String androidVersionId) {
        BaseVo result = new BaseVo();
        if(StringUtils.isBlank(androidVersionId) || !StringUtils.isNumeric(androidVersionId)){
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        AndroidVersion androidVersion = androidVersionService.getAndroidVersion(Long.parseLong(androidVersionId));
        if (androidVersion == null) {
            result.setCode(VersionConstant.RESPONSE_ANNDROID_VERSION_EXIST_ERROR_CODE);
            result.setMsg(VersionConstant.RESPONSE_ANNDROID_VERSION_EXIST_ERROR_MSG);
        }
        AndroidVersionInfoVo androidVersionInfoVo = new AndroidVersionInfoVo(androidVersion);
        result.setData(androidVersionInfoVo);
        return result;
    }


}
